import type { Emptyable, Numberic, Options, Percent, ProjectPerformance, Sizeic } from "../types";

/**
 * 计算页面样式和数值
 */
export class PtStyle {
    private performance: ProjectPerformance;

    constructor(performance: ProjectPerformance) {
        this.performance = performance;
    }

    public calcSize(size: Emptyable<Numberic | Percent, '' | undefined>): string {
        if (typeof size == 'number') {
            size = String(size) as Percent;
        }

        if (size == undefined) {
            return '';
        }

        const stringSize = String(size);

        // 百分比时，直接返回
        if (stringSize.endsWith('%')) {
            return stringSize;
        }

        // 以下字符开头的，按照特殊规则处理
        if (stringSize.startsWith('::')) {
            // 特殊处理，比如居中对齐
        }

        // 默认处理为 px
        return this.balanceSize(size) + 'px';
    }

    public balanceSize(size: Emptyable<Numberic | Percent, '' | undefined>): number {
        const ratio = this.getClientWidth() / Number(this.performance.width);

        return Number(size) * ratio;
    }

    public getClientWidth(): number {
        return Math.min(Number(this.performance.width), (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth));
    }

    public getRadiusSize(borderRadius: Emptyable<Numberic | Percent, '' | undefined>) {
        return borderRadius ? borderRadius == -1 ? '50%' : this.calcSize(borderRadius) : '';
    }

    public getAlignStyle(options: Options, parentX: Sizeic) {
        // 定位参考系
        if (options.positionAlign) {
            switch (options.positionAlign) {
                case 'left':
                    return '0';
                case 'center':
                    return `calc(100% / 2 - ${this.calcSize(options.width)} / 2)`;
                case 'right':
                    return `calc(100% - ${this.calcSize(options.width)})`;
            }
        }

        let left = this.calcSize(parentX);

        // 自身参考系
        if (options.originAlign) {
            if (options.originAlign == 'right') {
                left = `calc(${left} - ${this.calcSize(options.width)})`;
            } else if (options.originAlign == 'center') {
                left = `calc(${left} - ${this.calcSize(options.width)} / 2)`;
            }
        }

        return left;
    }

    public getVerticalStyle(options: Options, parentY: Sizeic) {
        if (options.positionVertical) {
            switch (options.positionVertical) {
                case 'top':
                    return '0';
                case 'middle':
                    return `calc(100% / 2 - ${this.calcSize(options.height)} / 2)`;
                case 'bottom':
                    return `calc(100% - ${this.calcSize(options.height)})`;
            }
        }

        let top = this.calcSize(parentY);

        // 修改自身参考系
        if (options.originVertical) {
            if (options.originVertical == 'bottom') {
                top = `calc(${top} - ${options.height})`;
            } else if (options.originVertical == 'middle') {
                top = `calc(${top} - ${options.height} / 2)`;
            }
        }

        return top;
    }
}